<?php

/*
  
  Project: CI Forum
  Type: PHP and database
  Desc: Contains all functions related to categories.
  Author: Yash Bharadwaj
 
  Function name : 
  view_forum     -> get value from database and show threads inside that forum.
  view_category  -> get value from database and show forums inside that category.
  view_thread    -> get value from database and show thread details along with posts.
  

*/

class Category_model extends Model {

    function Category_model() {
    // Call the Model constructor
        parent::Model();
        $this->load->helper('date');
        $this->load->library('table');

    }

    //save category to database.
    function create_category() {

        $insert = array(
            'category_id' => '',
            'category_title'  => $this->input->post('category_title'),
            'url_title'	=> url_title($this->input->post('category_title')),
            'category_desc' =>$this->input->post('category_desc')
        );

        $check= $this->db->insert('board_categories', $insert);
        $data['title']="Information Page";
        $data['header']="Information";

        if($check) {
            $data['time']="2";
            $data['info']="<p>Category is created Succesfully</p><p>Wait for auto redirecting</p>";
            $data['url']="admin/manage_categories";
        }
        else {
            $data['time']="12";
            $data['info']="<p>Creating Category Failed</p><p>Wait for auto redirecting</p>";
            $data['url']="admin/create_category/";
        }

        $this->load->view('info',$data);
    }

    function list_categories() {
        
        $query = $this->db->get('board_categories');
        return $query->result_array() ;
        /*$strr='<select name="cat_title">';
        foreach ($query->result() as $row) {	if($cat_title==$row->url_title)
                $strr.='<option selected="selected" value="'.$row->url_title.'">'.$row->category_title.'</option>';
            else
                $strr.='<option value="'.$row->url_title.'">'.$row->category_title.'</option>';
        }

        $strr.="</select>";
        return $strr;*/
    }
    function manage_categories() {

        $page_id=$this->uri->segment(3);
        $page_id=self::RefinePagNav($page_id);

        $this->db->from('board_categories');
        $cnt=$this->db->count_all_results();

        $config['base_url'] = site_url().'/admin/manage_categories/';
        $config['total_rows'] =$cnt;
        $config['per_page'] = '10';
        $config['num_links'] = '3';
        $config['uri_segment'] = '3';


        $this->pagination->initialize($config);
        $this->db->order_by("category_title", "desc");
        $query = $this->db->get('board_categories',$config['per_page'],$this->uri->segment(3));

        $out= "<table width=\"100%\" border=\"0\">";
        $out.="<caption>Categories List</caption>
               <thead>
                    <tr>
			<th scope=\"col\">Category Title</th>
			<th scope=\"col\">Category Desc</th>
			<th scope=\"col\">Edit</th>
			<th scope=\"col\">Delete</th>
                    </tr>
		</thead>
		<tfoot>
                    <tr>
			<th scope=\"row\">Total</th>
			<td colspan=\"3\">$cnt Posts</td>
                    </tr>
		</tfoot>
		<tbody>";


        foreach ($query->result() as $row) {
            $out.="<tr>
			    <td>".$row->category_title."</td>
			    <td>".$row->category_desc."</td>
			    <td>".anchor('admin/edit_category/'.$row->category_id,'Edit')."</td>
			    <td>".anchor('admin/delete_category/'.$row->category_id.'/'.$page_id,'Delete')."</td>
			  </tr>";


        }
        $out.= "</tbody></table>";
        $out.= $this->pagination->create_links();
        return $out;

    }

    function delete_category() {

        $category_id= $this->uri->segment(3);
        $page_id= $this->uri->segment(4);

        $page_id=self::RefinePagNav($page_id);
        $this->db->where('category_id', $category_id);
        $check=$this->db->delete('board_categories');

        if($check) {
            $info['info']="<p>category successfully deleted.<p><p>Wait for auto redirecting</p>";
        }
        else {
            $info['info']="<p>Unexpected Error...Deletion Failed.<p>";
        }

        $info['title']="Information Page";
        $info['header']="Information";
        $info['url']='/admin/manage_categories/'.$page_id;
        $info['time']="2";

        $this->load->view('info',$info);
    }

    function edit_category() {

        $category_id= $this->uri->segment(3);
        $page_id= $this->uri->segment(4);

        $update = array(
            'category_title' => $this->input->post('category_title'),
            'url_title' => url_title($this->input->post('category_title')),
            'category_desc' => $this->input->post('category_desc'),
        );
        $this->db->where(array('category_id'=>$category_id));
        $check=$this->db->update('board_categories', $update);
        if($check) {
            $info['info']="<p>category successfully updated.<p><p>Wait for auto redirecting</p>";
        }
        else {
            $info['info']="Unexpected Error...Updation Failed.";

        }

        $info['title']="Information Page";
        $info['header']="Information";
        $info['url']="/admin/Edit_category/".$category_id;
        $info['time']="2";

        $this->load->view('info',$info);


    }


    function RefinePagNav($page_id) {
        if($page_id==1)
            $page_id=0;

        return $page_id;
    }



}?>